package com.xyz.controller;

import com.xyz.aop.ObjectParam;
import com.xyz.aop.Param;
import com.xyz.config.SystemData;
import com.xyz.dto.*;
import com.xyz.service.RepairService;
import com.xyz.service.WeChatService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.LoginInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("weChat")
public class WeChatController {

    private static final Logger logger = LoggerFactory.getLogger(WeChatController.class);

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private RepairService repairService;

    /*
     * 报修
     * */
    @PostMapping("/insertRepair")
    public DataResult insertRepair(LoginInfo loginInfo, @ObjectParam RepairWorkOrderDto repairDto) {
        try {
            synchronized (SystemData.repairLock) {
                return repairService.insertRepair(loginInfo, repairDto);
            }
        } catch (Exception e) {
            logger.error("WeChat/insertRepair", e);
            return DataResult.build9500();
        }
    }

    /*
     * 首页-列表数
     * */
    @PostMapping("/getHomeRepairCount")
    public DataResult getRepairCount(LoginInfo loginInfo) {
        try {
            return weChatService.getHomeRepairCount(loginInfo);
        } catch (Exception e) {
            logger.error("WeChat/getHomeRepairCount", e);
            return DataResult.build9500();
        }
    }

    /*
    * 查询列表
    * */
    @PostMapping("/getRepairAll")
    public DataResult getRepairAll(LoginInfo loginInfo, @ObjectParam WeChatPageAllDto weChatPageAllDto) {
        try {
            return weChatService.getRepairAll(loginInfo,weChatPageAllDto);
        } catch (Exception e) {
            logger.error("WeChat/getRepairAll", e);
            return DataResult.build9500();
        }
    }

    /*
     * 维修评价
     * */
    @PostMapping("/repairEvaluate")
    public DataResult repairEvaluate(LoginInfo loginInfo, @ObjectParam RepairEvaluateDto repairEvaluateDto) {
        try {
            return weChatService.repairEvaluate(loginInfo,repairEvaluateDto);
        } catch (Exception e) {
            logger.error("WeChat/repairEvaluate", e);
            return DataResult.build9500();
        }
    }

    /*
     * 查询维修评价
     * */
    @PostMapping("/getRepairEvaluateAll")
    public DataResult getRepairEvaluateAll(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageNum,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String pageSize
    ) {
        try {
            return weChatService.getRepairEvaluateAll(loginInfo,pageNum,pageSize);
        } catch (Exception e) {
            logger.error("WeChat/getRepairEvaluateAll", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询报修单基本信息
     * */
    @PostMapping("/getBasicRepairById")
    public DataResult getBasicRepairById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId
    ) {
        try {
            return repairService.getBasicRepairById(loginInfo, repId);
        } catch (Exception e) {
            logger.error("WeChat/getBasicRepairById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询报修单审核信息
     * */
    @PostMapping("/getAnnounceRepairById")
    public DataResult getAnnounceRepairById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repState
    ) {
        try {
            return repairService.getAnnounceRepairById(loginInfo, repId, repState);
        } catch (Exception e) {
            logger.error("WeChat/getAnnounceRepairById", e);
            return DataResult.build9500();
        }
    }


    /*
     * 根据ID查询报修单维修信息
     * */
    @PostMapping("/getMaintenanceRepairById")
    public DataResult getMaintenanceRepairById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repState
    ) {
        try {
            return repairService.getMaintenanceRepairById(loginInfo, repId, repState);
        } catch (Exception e) {
            logger.error("WeChat/getMaintenanceRepairById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据维修ID查询验收信息
     * */
    @PostMapping("/getAcceptById")
    public DataResult getAcceptById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repId,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String repState
    ) {
        try {
            return repairService.getAcceptById(loginInfo, repId, repState);
        } catch (Exception e) {
            logger.error("WeChat/getAcceptById", e);
            return DataResult.build9500();
        }
    }
}
